package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Equipment;
import com.ruoyi.system.service.IEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备数据Controller
 * 
 * @author gzhuangwei
 * @date 2022-09-17
 */
@Api("设备管理")
@RestController
@RequestMapping("/business/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设备数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }

    /**
     * 导出设备数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:export')")
    @Log(title = "设备数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.exportExcel(response, list, "设备数据数据");
    }

    /**
     * 获取设备数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(equipmentService.selectEquipmentById(id));
    }

    /**
     * 新增设备数据
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:add')")
    @Log(title = "设备数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备数据
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:edit')")
    @Log(title = "设备数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备数据
     */
    @PreAuthorize("@ss.hasPermi('business:equipment:remove')")
    @Log(title = "设备数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(equipmentService.deleteEquipmentByIds(ids));
    }

    /**
     * 1.2.1
     * 1.2.2
     * 患者绑定设备,绑定时传入正确的userid，解绑传0
     * param userid  患者id
     * param equipmentID  设备id
     */
    @ApiOperation("患者解绑/绑定设备")
    @Log(title = "设备数据", businessType = BusinessType.UPDATE)
    @PostMapping("/editUserId")
    public AjaxResult editUserId(@RequestParam(name = "userId")String userId , @RequestParam(name = "equipmentId")String equipmentId)
    {
        //通过设备id查询
        Equipment equipment = equipmentService.selectEquipmentByEquipmentId(equipmentId);
        //修改userid
        equipment.setUserid(userId);

        return toAjax(equipmentService.updateEquipment(equipment));
    }
    /**
     * 1.3.1
     * 查询患者绑定的设备列表
     */
    @ApiOperation("查询患者绑定的设备列表")
    @GetMapping("/equipmentList")
    public AjaxResult  list(@RequestParam("userId") String userId)
    {
        List<Equipment> list = equipmentService.selectEquipmentByUserId(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("equipmentList", list);

        return ajax;
    }
}
